package com.zh.edu.info.manager.controller;

import com.zh.edu.info.manager.domain.Student;
import com.zh.edu.info.manager.domain.Teacher;
import com.zh.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {
        ol:
        while (true) {
            System.out.println("--------欢迎来到zh<教师>信息管理系统--------");
            System.out.println("1.添加教师   2.删除教师   3.修改教师   4.查询教师   5.退出");
            System.out.print("请输入您的选择：");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    selectTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    showTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用教师管理系统，再见");
                    break ol;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }

    }

    private void updateTeacher() {
        String id = inputId();
        Teacher teacher = inputTeacher(id);
        teacherService.updateTeacher(id, teacher);
        System.out.println("修改成功！");
    }

    private void selectTeacher() {
        String id = inputId();
        teacherService.selectTeacher(id);
        System.out.println("删除成功！");
    }

    private String inputId() {
        System.out.println("请输入教师ID：");
        String id;
        while (true) {
            id = sc.next();
            boolean flog = teacherService.idFind(id);
            if (flog) {
                break;
            }else {
                System.out.println("教师ID不存在，请重新输入：");
            }
        }
        return id;
    }

    private void showTeacher() {
        System.out.println("教师id\t\t姓名\t\t年龄\t\t生日");
        Teacher[] teacher = teacherService.showTeacher();
        if (teacher == null) {
            System.out.println("查无信息，请添加后重试");
        }else {
            for (int i = 0; i < teacher.length; i++) {
                if (teacher[i] != null) {
                    System.out.println(teacher[i].getId() + "\t\t" + teacher[i].getName() + "\t\t" + teacher[i].getAge() + "\t\t" + teacher[i].getBirthday());
                }
            }
        }
    }

    private void addTeacher() {
        String id;
        System.out.println("请输入教师的ID：");
        while (true) {
            id = sc.next();
            boolean flog = teacherService.idFind(id);
            if (flog) {
                System.out.println("教师id已被占用，请重新输入:");
            } else {
                break;
            }
        }
        Teacher teacher = inputTeacher(id);
        teacherService.addTeacher(teacher);
        System.out.println("添加成功！");
    }

    private Teacher inputTeacher(String id) {
        System.out.println("请输入教师姓名：");
        String name = sc.next();
        System.out.println("请输入教师年龄：");
        int age = sc.nextInt();
        System.out.println("请输入教师的生日：");
        String birthday = sc.next();
        Teacher teacher = new Teacher(id, name, age, birthday);
//        teacher.setId(id);
//        teacher.setName(name);
//        teacher.setAge(age);
//        teacher.setBirthday(birthday);
        return teacher;
    }
}
